var mongoose = require('mongoose');
var Schema = mongoose.Schema;
var ObjectId = Schema.ObjectId;
var config = require('../config');
var _ = require('lodash');
var autoIncrement = require('mongoose-auto-increment');


autoIncrement.initialize(mongoose.connection);

var TopicSchema = new Schema({
    title: {
        type: String
    },
    content: {
        type: String
    },
    author_id: {
        type: ObjectId
    },
    top: {
        type: Boolean,
        default: false
    }, // 置顶帖
    good: {
        type: Boolean,
        default: false
    }, // 精华帖
    reply_count: {
        type: Number,
        default: 0
    },
    visit_count: {
        type: Number,
        default: 0
    },
    collect_count: {
        type: Number,
        default: 0
    },
    create_at: {
        type: Date,
        default: Date.now
    },
    update_at: {
        type: Date,
        default: Date.now
    },
    last_reply: {
        type: ObjectId
    },
    last_reply_at: {
        type: Date,
        default: Date.now
    },
    content_is_html: {
        type: Boolean
    },
    tab: {
        type: String
    },
    // add eam properties
    // AlarmID (table alarm; field: alarmID)
    // WindTurbineID(table alarm (foreign key to wt); field:wtID)
    // componentID(table alarm; field: componentID)
    // rootCause(table alarm; field: rootCause)
    // timeStamp(table alarm; field: timeStamp)
    // alarmLevel(table alarm; field: alarmLevel)
    eam_wo_id: {
        type: Number,
        required: false
    },
    // category == tab
    eam_wo_category: {
        type: String,
        required: false
    },
    eam_wind_turbine_id: {
        type: String
    },
    eam_component_id: {
        type: String
    },
    eam_root_cause: {
        type: String
    },
    eam_root_cause_insp: {
        type: String
    },
    eam_root_cause_maintain: {
        type: String
    },
    eam_timestamp: {
        type: Date,
        default: Date.now,
        required: false
    },
    eam_alarm_level: {
        type: Number
    },
    eam_maintain_toolkits: {
        type: Schema.Types.Mixed
    },
    eam_inspection_assignee: {
        // alarm page, push to this worker
        // TODO associate eam_inspection_assignee as a foreign key
        // type: Schema.Types.ObjectId
        type: String
    },
    eam_maintain_assignee: {
        type: String
    },
    eam_repair_guide: {
        type: String
    },
    eam_maintain_parts: {
        type: Schema.Types.Mixed
    },
    eam_maintain_from_insp_id: {
        type: Schema.Types.ObjectId,
        ref: 'Topic'
    },
    eam_report_from_maintain_id: {
        type: Schema.Types.ObjectId,
        ref: 'Topic'
    },
    // 0 : created inspection wo
    // 1 : assigned inspection wo
    // 2 : closed inspection wo
    // 3 : pending maintenance wo
    // 4 : assigned maintenance wo
    // 5 : closed maintenance wo
    // 6 : report
    eam_wo_status: {
        type: Number,
        default: 0
    }
});

TopicSchema.index({
    create_at: -1
});
TopicSchema.index({
    top: -1,
    last_reply_at: -1
});
TopicSchema.index({
    last_reply_at: -1
});
TopicSchema.index({
    author_id: 1,
    create_at: -1
});

TopicSchema.virtual('tabName').get(function() {
    var tab = this.tab;
    var pair = _.find(config.tabs, function(_pair) {
        return _pair[0] === tab;
    });
    if (pair) {
        return pair[1];
    } else {
        return '';
    }
});

TopicSchema.plugin(autoIncrement.plugin, {
    model: 'Topic',
    field: 'eam_wo_id',
    startAt: 100
});

mongoose.model('Topic', TopicSchema);
